package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    Button button;
    Button button2;
    Button button3;

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button4);
        editText = findViewById(R.id.editTextText);

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }


    /**
     * 按钮的事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        Log.d(TAG, "onClick: "+id);
        if(view instanceof Button){
            Button btn = (Button) view;
            CharSequence text = btn.getText();
            if(text.equals("Button")){
                Log.d(TAG, "onClick: "+text);
            }else if(text.equals("button2")){
                Log.d(TAG, "onClick: "+text);
            }else if(text.equals("打开一个新的页面")){
                // 跳转到第二个界面
                Intent intent = new Intent(this, MainActivity2.class);
                String s = editText.getText().toString();
                if(s != null && !s.equals("")){
                    intent.putExtra("text",s);
                }else{
                    intent.putExtra("text","我是一个努力工作的人");
                }
                startActivity(intent);
            }
        }
    }
}